package model.hospital;

import model.Database;
import model.UID_Generator;
import model.assessment.Test;
import net.passClass.PassData;
import net.passClass.PassPatient;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

public class PatientDB implements Database<Patient> {
    private ArrayList<Patient> patients;

    public ArrayList<Patient> getPatients() {
        return patients;
    }
    private static PatientDB instance = null;
    private PatientDB() {}
    private PatientDB(ArrayList<Patient> patients) {
        if(patients!=null){
            this.patients = patients;
        } else {
            this.patients = new ArrayList<>();
        }
    }
    public static PatientDB getInstance() {
        if (instance == null){
            instance = new PatientDB();
        }
        return instance;
    }
    public static void initialize(ArrayList<Patient> patients) {
        if (instance == null){
            instance = new PatientDB(patients);
        }
    }

    public PassData test(String uid, Date time, String templateUID, int mark){
        ArrayList<Patient> patients = get(new Patient(uid)).getData();
        if (patients == null){
            return new PassData(false,null,"病人不存在");
        }
        Patient aPatient = patients.get(0);
        aPatient.addTest(new Test(templateUID,time,mark));
        return new PassData(true, null,"测试成功");
    }

    @Override
    public PassData add(Patient patient){
        patient.setUid(UID_Generator.getUid("P"));
        patients.add(patient);
        return new PassData(true, null,"添加成功");
    }

    @Override
    public PassData update(Patient patient) {
        if(!patients.contains(patient)){
            return new PassData(false, null,"病人不存在，修改失败");
        }
        Patient thePatient = patients.get(patients.indexOf(patient));
        thePatient.setDeleted(true);
        patients.add(patient);
        return new PassData(true, null,"修改成功");
    }

    @Override
    public PassData delete(Patient patient) {
        if(!patients.contains(patient)){
            return new PassData(false, null,"病人不存在，删除失败");
        }
        Patient thePatient = patients.get(patients.indexOf(patient));
        thePatient.setDeleted(true);
        return new PassData(true, null,"删除成功");
    }

    @Override
    public PassPatient get(Patient patient) {
        LinkedList<Patient> tmpAns = new LinkedList<>(patients);
        tmpAns.removeIf(Patient::isDeleted);

        if(patient.getUid()!=null) {
            if (tmpAns.contains(patient)) {
                ArrayList<Patient> ans = new ArrayList<>();
                ans.add(tmpAns.get(tmpAns.indexOf(patient)));
                return new PassPatient(true, ans, "查找成功");
            } else {
                return new PassPatient(true, null, "不存在查找结果");
            }
        }

        if(patient.getName()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getName()==null ||
                    !aPatient.getName().contains(patient.getName()));
        }
        if(patient.getBedUID()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getBedUID()==null ||
                    !aPatient.getBedUID().contains(patient.getBedUID()));
        }
        if(patient.getIdNumber()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getIdNumber()==null ||
                    !aPatient.getIdNumber().contains(patient.getIdNumber()));
        }
        if(patient.getBirthday()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getBirthday()==null ||
                    !aPatient.getBirthday().equals(patient.getBirthday()));
        }
        if(patient.getTel()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getTel()==null ||
                    !aPatient.getTel().contains(patient.getTel()));
        }
        if(patient.getDoctorUID()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getDoctorUID()==null ||
                    !aPatient.getDoctorUID().contains(patient.getDoctorUID()));
        }
        if(patient.getEmergencyContact()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getEmergencyContact()==null ||
                    !aPatient.getEmergencyContact().contains(patient.getEmergencyContact()));
        }
        if(patient.getEmergencyTel()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getEmergencyTel()==null ||
                    !aPatient.getEmergencyTel().contains(patient.getEmergencyTel()));
        }
        if(patient.getSex()!=null){
            tmpAns.removeIf(aPatient -> aPatient.getSex()==null ||
                    !aPatient.getSex().contains(patient.getSex()));
        }

        ArrayList<Patient> ans = new ArrayList<>(tmpAns);
        return new PassPatient(true, ans,"查找成功");
    }
}
